package com.weibo.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.weibo.tool.HttpTool;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class MessageService {


    @Test
    public void test() throws Exception{
        MessageService messageService = new MessageService();
        //messageService.send("加油","2294486453");
        System.out.println(messageService.queryMsgs("2723136710"));
    }

    public boolean send(String text,String uid) throws Exception {

        String url ="https://api.weibo.com/webim/2/direct_messages/new.json";
        JSONObject extensions = new JSONObject();
        extensions.put("clientid","hmf3br0h0pwjx5fme1beil976avhfv");

        List<NameValuePair> urlParameters = new ArrayList<>();
        urlParameters.add(new BasicNameValuePair("text", text));
        urlParameters.add(new BasicNameValuePair("uid", uid));
        urlParameters.add(new BasicNameValuePair("is_encoded", "0"));
        urlParameters.add(new BasicNameValuePair("decodetime", "1"));
        urlParameters.add(new BasicNameValuePair("source", "209678993"));
        urlParameters.add(new BasicNameValuePair("extensions", JSON.toJSONString(extensions)));


        JSONObject res = (JSONObject)HttpTool.post(url,null,urlParameters);
        System.out.println(res.toString());

        return true;
    }

    public int queryMsgs(String uid) throws Exception {

        String url = "https://api.weibo.com/webim/2/direct_messages/conversation.json?convert_emoji=1&count=15&max_id=0&is_include_group=0&from_contacts=1&source=209678993&uid="+uid+"&t="+System.currentTimeMillis();
        JSONObject res = (JSONObject)HttpTool.get(url);
        return res.getJSONArray("direct_messages").size();
    }
}
